//
//  UUFleetPicker.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 26/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUFleetPicker.h"

@interface UUFleetPicker ()

@property (nonatomic, strong) NSMutableOrderedSet<NSNumber *> *selectedFleetIds;
@property (nonatomic, assign) BOOL allowsMultipleSelection;

@property (nonatomic, copy) void (^didSelectFleetBlock)(NSArray<NSNumber *> *selectedFleetIds);

@end

@implementation UUFleetPicker

static UIViewController *_picker;
+ (UIViewController *)pickerWithSelectedFleetIds:(NSArray<NSNumber *> *)selectedFleetIds
                         allowsMultipleSelection:(BOOL)allowsMultipleSelection
                                   completeBlock:(void (^)(NSArray<NSNumber *> *selectedFleetIds))completeBlock {
    UUFleetPicker *picker = [self instanceOfAppropriate];
    picker.allowsMultipleSelection = allowsMultipleSelection;
    picker.selectedFleetIds = [NSMutableOrderedSet orderedSetWithArray:selectedFleetIds];
    picker.title = @"请选择车队";
    WEAK_SELF();
    picker.didSelectFleetBlock = ^(NSArray<NSNumber *> *selectedFleetIds) {
        completeBlock(selectedFleetIds);
        [weakSelf dismiss];
    };
    picker.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"取消"
                                                                               target:self
                                                                             selector:@selector(dismiss)];
    _picker = picker;
    return [[UINavigationController alloc] initWithRootViewController:picker];
}

+ (void)dismiss {
    [_picker dismissViewControllerAnimated:YES completion:nil];
    _picker = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.allowsMultipleSelection) {
        self.tableView.allowsMultipleSelection = YES;
        
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"确定"
                                                                                  target:self
                                                                                selector:@selector(done:)];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)done:(id)sender {
    self.didSelectFleetBlock([self.selectedFleetIds array]);
}

- (BOOL)canAddFleet {
    return NO;
}

- (void)configCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.allowsMultipleSelection) {
        NSNumber *fleetId = [self fleetIdForIndexPath:indexPath];
        if ([self.selectedFleetIds containsObject:fleetId]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSNumber *fleetId = [self fleetIdForIndexPath:indexPath];
    
    if (self.allowsMultipleSelection) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [self.selectedFleetIds addObject:fleetId];
        } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [self.selectedFleetIds removeObject:fleetId];
        }
    } else {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        BLOCK_SAFE_CALLS(self.didSelectFleetBlock, @[fleetId]);
    }
}

@end
